import Cookie from 'js-cookie'

import router from '@/router/index.js'; // 假设您的路由实例已经在外部文件中定义并导出了
import store from '@/store/index.js';
import Main from '../views/Main.vue'
import Import from '../views/main/Import.vue'
import Analysis2 from '../views/main/Analysis2.vue'
import Result from '../views/main/Result.vue'
import Relation from '../views/main/Relation.vue'

export default {
    state: {
        isImported: false,

        menuData_import: [
            {
                path: '/main/import',
                name: 'import',
                label: '数据导入',
                url: '/import',
                icon: 'upload'
            },
            {
                label: '欺诈预测',
                icon: 'data-analysis',
                children: [
                    {
                        path: '/main/result',
                        name: 'result',
                        label: '欺诈结果显示',
                        url: '/result'
                    },
                    {
                        path: '/main/relation',
                        name: 'relation',
                        label: '识别欺诈的重要特征',
                        url: '/relation'
                    }
                ]
            },
            {
                label: '数据分析',
                icon: 's-data',
                children: [
                    // {
                    //     path: '/main/analysis1',
                    //     name: 'analysis1',
                    //     label: '整体数据分析',
                    //     url: '/analysis1'
                    // },
                    {
                        path: '/main/analysis2',
                        name: 'analysis2',
                        label: '数据统计',
                        url: '/analysis2'
                    }
                ]
            },
        ],
        menuData_unimport: [
            {
                path: '/main/import',
                name: 'import',
                label: '数据导入',
                url: '/import',
                icon: 'upload'
            },
        ],

        route_import: {
            path: '/main/',
            name: 'main',
            component: Main,
            redirect: '/main/import', // 重定向
            beforeEnter: (to, from, next) => {
                // 在根路由'/main/'进入前执行初始化操作
                store.commit('initUserData'); // 使用引入的store实例
                next();
            },
            children: [
                // 子路由
                { path: 'import', component: Import },
                // { path: 'analysis1', component: Analysis1 },
                { path: 'analysis2', component: Analysis2 },
                { path: 'result', component: Result },
                { path: 'relation', component: Relation }
            ]
        },
        route_unimport: {
            path: '/main/',
            name: 'main',
            component: Main,
            redirect: '/main/import', // 重定向
            beforeEnter: (to, from, next) => {
                // 在根路由'/main/'进入前执行初始化操作
                store.commit('initUserData'); // 使用引入的store实例
                next();
            },
            children: [
                // 子路由
                { path: 'import', component: Import },
            ]
        }
    },
    mutations: {
        setImport(state, val) {
            state.isImported = val;
            Cookie.set('isImported', val, { expires: 1 });

            // 修改路由
            if (val === true) {
                console.log(1);
                router.addRoute(state.route_import); // 添加导入数据时的路由配置
            } else {
                console.log(2);
                router.addRoute(state.route_unimport); // 添加未导入数据时的路由配置
            }
        },
        initImport(state) {
            const isImported = Cookie.get('isImported');
            // isImported是string
            if (isImported) {
                if (isImported === 'true') {
                    state.isImported = true;
                } else {
                    state.isImported = false;
                }
            }
        },
        initRouter(state) {
            const isImported = Cookie.get('isImported');
            // isImported是string
            if (isImported) {
                if (isImported === 'true') {
                    router.addRoute(state.route_import); // 添加导入数据时的路由配置
                } else {
                    router.addRoute(state.route_unimport); // 添加未导入数据时的路由配置
                }
            }
        }
    }
}